﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

using MM.Entities;
using MM.Business;

using Word1 = Microsoft.Office.Interop.Word;
using System.Reflection;

namespace MM
{
    public partial class frmHienthiPhieuYC : Form
    {
        
        string _matau = "";
        string _nguoitao = "";
        string _maphieuyeucau = "";
        string _sConn = "";

        public frmHienthiPhieuYC(string maphieuyeucau,string sConn)
        {
            InitializeComponent();

            _matau = frmDangNhap.objTau._tenTau2KiTu;

            if (maphieuyeucau == "")
            {
                // tim thong tin phieu yeu cau gan nhat
                clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(sConn);
                List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
                lst = pycCtrl.SelectPHIEU_YEU_CAU_MAX(_matau);
                if (lst.Count > 0)
                    maphieuyeucau = lst[0].MaPhieuYeuCau;
            }

            _maphieuyeucau = maphieuyeucau;
            _sConn = sConn;

            this.HienthiPHIEU_YEU_CAU();
            this.HienthiCHI_TIET_PYC();
            this.HienthiCHI_TIET_GT_ND();
        }
        public frmHienthiPhieuYC(string sConn)
        {
            InitializeComponent();

            _matau = frmDangNhap.objTau._tenTau2KiTu;

          
            // tim thong tin phieu yeu cau gan nhat
            clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(sConn);
            List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
            lst = pycCtrl.SelectPHIEU_YEU_CAU_MAX(_matau);
            if (lst.Count > 0)
                _maphieuyeucau = lst[0].MaPhieuYeuCau;         

          
            _sConn = sConn;

            this.HienthiPHIEU_YEU_CAU();
            this.HienthiCHI_TIET_PYC();
            this.HienthiCHI_TIET_GT_ND();
        }
        
        private void HienthiPHIEU_YEU_CAU()
        {
            clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(_sConn);
            List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
            lst = pycCtrl.SelectPHIEU_YEU_CAU(_maphieuyeucau);
            if (lst.Count > 0)
            {                
                this.txtMaPhieuYeuCau.Text = lst[0].MaPhieuYeuCau;
                this.txtNguoiThucHien.Text = lst[0].NguoiTao;
                this.txtNgayThucHien.Text = lst[0].NgayTao.Day.ToString() + "/" + lst[0].NgayTao.Month.ToString() + "/" + lst[0].NgayTao.Year.ToString();
                if (lst[0].DaDuyet==1)
                    chkDaduyet.Checked = true;
                else
                    chkDaduyet.Checked = false;

                if (lst[0].TinhTrang == 1)
                    chkXoa.Checked = true;
                else
                    chkXoa.Checked = false;
            }
        }
        private void HienthiCHI_TIET_PYC()
        {
            List<CHI_TIET_PYC> lstCT = new List<CHI_TIET_PYC>();

            clsCHI_TIET_PYC ctCtrl = new clsCHI_TIET_PYC(_sConn);
            clsCHI_TIET_PDH_COMPONENT comCtrl = new clsCHI_TIET_PDH_COMPONENT(_sConn);
            List<CHI_TIET_PDH_COMPONENT> lstCom;

            lstCT = ctCtrl.SelectCHI_TIET_PYCByMaPhieuYeuCau(_maphieuyeucau);
            for (int i = 0; i < lstCT.Count; i++)
            {                
                if (lstCT[i].MaTrangThai == 1)// gia cong
                {
                    lstCom = new List<CHI_TIET_PDH_COMPONENT>();
                    lstCom = comCtrl.SelectCHI_TIET_PYC_COMPONENT(lstCT[i].MaChiTiet);
                    lstCT[i].lstComponent = lstCom;
                }            
            }
            this.gridControl1.DataSource = lstCT;
        }
        private void HienthiCHI_TIET_GT_ND()
        {
            try
            {
                clsCHI_TIET_ND_GT gtCtrl = new clsCHI_TIET_ND_GT(_sConn);
                List<CHI_TIET_ND_GT> lstGT = new List<CHI_TIET_ND_GT>();
                lstGT = gtCtrl.usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(_maphieuyeucau);

                this.gridControl2.DataSource = lstGT;
            }
            catch { }

        }

        private void frmHienthiPhieuYC_Paint(object sender, PaintEventArgs e)
        {
            //Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void label1_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;           
        }

        private void label1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuYC f = new frmTimPhieuYC(_sConn);
            f.chonPYC += new frmTimPhieuYC.ChonPhieuYeuCau(GetPhieuYeuCau);            
            f.Show(this);
            
 
        }
        private void GetPhieuYeuCau(object sender, ChonPhieuYCEventArgs e)
        {
            _maphieuyeucau = e.MaPhieuYeuCau;

            this.HienthiPHIEU_YEU_CAU();
            this.HienthiCHI_TIET_PYC();
            this.HienthiCHI_TIET_GT_ND();
        }

        private void frmHienthiPhieuYC_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void txtMaPhieuYeuCau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                _maphieuyeucau = this.txtMaPhieuYeuCau.Text;

                this.HienthiPHIEU_YEU_CAU();
                this.HienthiCHI_TIET_PYC();
                this.HienthiCHI_TIET_GT_ND();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;
            //Start Word and create a new document.
            Word1._Application oWord;
            Word1._Document oDoc;

            oWord = new Word1.Application();
            oWord.Visible = true;

            object oTemplate = @"D:\Working\MM On Board\data for test\10-01-GE-02.dot";
            oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
                ref oMissing, ref oMissing);

            object oBookMark = "bTable";

            Word1.Table oTable;
            Word1.Range wrdRng = oDoc.Bookmarks.get_Item(ref oBookMark).Range;
            int rows = 3;
            int cols = 6;
            oTable = oDoc.Tables.Add(wrdRng, rows, cols, ref oMissing, ref oMissing);
            oTable.Range.ParagraphFormat.SpaceAfter = 6;
            int r, c;
            string strText;
            for (r = 3; r <= rows + 2; r++)
                for (c = 1; c <= cols; c++)
                {
                    strText = "r" + r + "c" + c;
                    oTable.Cell(r, c).Range.Text = strText;
                    if (c==1)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.37f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                    if (c==2)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.59f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                    if (c==3)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(2.26f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                    if (c==4)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(2.07f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                    if (c==5)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.89f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                    if (c==6)
                        oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.78f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);

                    oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderLeft].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                    oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleDot;
                    oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderRight].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                    oTable.Cell(r, c).Range.Font.Name = "VNI-Times";
                    oTable.Cell(r, c).Range.Font.Size = 10;
                    oTable.Cell(r, c).Range.Font.Underline = 0;
                }
            oTable.Rows[rows + 2].Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
           

        }

       
    }
}